package org.jsola.hr.web;

import org.apache.commons.lang3.StringUtils;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.IssuedSocProgAddDTO;
import org.jsola.hr.dto.SocProgAddDTO;
import org.jsola.hr.dto.SocProgBatchDTO;
import org.jsola.hr.dto.SocProgUpdateDTO;
import org.jsola.hr.exception.HrException;
import org.jsola.hr.query.SocProgQuery;
import org.jsola.hr.service.ISocProgService;
import org.jsola.hr.vo.SocProgListVO;
import org.jsola.hr.vo.SocProgVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 参保方案表
 *
 * @author zhr
 */
@RestController("hrSocProgController")
@RequestMapping(HrConstants.API_PATH + "/v1/socProgs")
public class SocProgController implements ISocProgControllerAPI {

    @Autowired
    private ISocProgService socProgService;

    @Override
    @PostMapping
    public Result<SocProgVO> save(@Valid @RequestBody SocProgAddDTO socProgAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增参保方案表权限校验

        return Result.success(
                socProgService.save(socProgAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody SocProgUpdateDTO socProgUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改参保方案表权限校验
        if (StringUtils.isBlank(socProgUpdateDTO.getProgName())) {
            throw new HrException("方案名称不可为空");
        }

        int count = socProgService.update(socProgUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @PostMapping(value = "/batch")
    public Result batch(@Valid @RequestBody SocProgBatchDTO socProgBatchDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量操作权限校验

        // 批量操作，注意校验，有越权风险
        // 新增
        List<SocProgAddDTO> socProgAddDTOList = socProgBatchDTO.getSocProgAddDTOList();
        if (!CollectionUtils.isEmpty(socProgAddDTOList)) {
            socProgService.batchSave(socProgAddDTOList, tokenUser);
        }
        // 修改
        List<SocProgUpdateDTO> socProgUpdateDTOList = socProgBatchDTO.getSocProgUpdateDTOList();
        if (!CollectionUtils.isEmpty(socProgUpdateDTOList)) {
            socProgUpdateDTOList.parallelStream().forEach(
                    socProgUpdateDTO -> {
                        socProgService.update(socProgUpdateDTO, tokenUser);
                    }
            );
        }
        // 删除
        List<String> deleteSocProgIdList = socProgBatchDTO.getDeleteSocProgIdList();
        if (!CollectionUtils.isEmpty(deleteSocProgIdList)) {
            socProgService.deleteByIds(tokenUser, deleteSocProgIdList.toArray(new String[0]));
        }
        return Result.success();
    }


    @Override
    @DeleteMapping(value = "/{socProgId}")
    public Result<Integer> delete(@PathVariable String socProgId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除参保方案表权限校验

        int count = socProgService.deleteByIds(tokenUser, socProgId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] socProgIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除参保方案表权限校验

        int count = socProgService.deleteByIds(tokenUser, socProgIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{socProgId}")
    public Result<SocProgVO> findById(@PathVariable String socProgId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看参保方案表权限校验

        return Result.success(
                socProgService.selectById(socProgId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<SocProgListVO>> listAll(@Valid SocProgQuery socProgQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看参保方案表权限校验

        return Result.success(
                socProgService.select(socProgQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping(value = "/listAllRelation")
    public Result<List<SocProgListVO>> listAllRelation(@Valid SocProgQuery socProgQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看参保方案表权限校验

        return Result.success(
                socProgService.listAllRelation(socProgQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<SocProgListVO>> page(@Valid SocProgQuery socProgQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                socProgService.selectPage(socProgQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "pageRelation")
    public Result<Page<SocProgListVO>> pageRelation(@Valid SocProgQuery socProgQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                socProgService.pageRelation(socProgQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "findSocProgRelation")
    public Result<SocProgVO> findSocProgRelation(@Valid String progId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                socProgService.findSocProgRelation(progId, tokenUser)
        );
    }


    @Override
    @PostMapping("/saveIssuedSocProg")
    public Result<List<SocProgVO>> saveIssuedSocProg(@Valid @RequestBody IssuedSocProgAddDTO issuedSocProgAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增参保方案表权限校验

        return Result.success(
                socProgService.saveIssuedSocProg(issuedSocProgAddDTO, tokenUser)
        );
    }

    @Override
    @GetMapping("/selectSchemeByEmpSocInfoId")
    public Result<List<String>> selectSchemeByEmpSocInfoId(@RequestParam("empSocInfoId") String empSocInfoId,
                                                           @RequestParam("companyId") String companyId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增参保方案表权限校验

        return Result.success(
                socProgService.selectSchemeByEmpSocInfoId(empSocInfoId, companyId, tokenUser)
        );
    }

    @Override
    @GetMapping("/selectSchemeByEmpRelated")
    public Result<SocProgVO> selectSchemeByEmpRelated(@RequestParam("empId") String empId,
                                                      @RequestParam("companyId") String companyId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增参保方案表权限校验

        return Result.success(
                socProgService.selectSchemeByEmpRelated(empId, companyId, tokenUser)
        );
    }

}

